#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define ll long long
#define fi first
#define se second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << (a) << " ";
#define debugl(a) cerr << #a << " = " << (a) << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second 
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

int n, m;
int a[323][323];

void load()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		nextLine();
		for (int j = 0; j < m; j++)
		{
			a[i][j] = getchar() - '0';
		}
	}

}

const ll X = 41;
const ll Y = 157;	

ll pwx[323];
ll pwy[323];

ll hash[323][323], rhash[323][323];


ll get_hash(int i, int j, int size)
{
	ll result = hash[i][j];
	if (i - size >= 0) result -= hash[i - size][j] * pwx[size];
	if (j - size >= 0) result -= hash[i][j - size] * pwy[size];
	if (i - size >= 0 && j - size >= 0) result += hash[i - size][j - size] * pwx[size] * pwy[size];
	return result;
}

ll get_hash_rev(int i, int j, int size)
{
	ll result = rhash[i][j];
	if (i + size < n) result -= rhash[i + size][j] * pwx[size];
	if (j + size < m) result -= rhash[i][j + size] * pwy[size];
	if (i + size < n && j + size < m) result += rhash[i + size][j + size] * pwx[size] * pwy[size];
	return result;
}


bool check(int size)
{
	for (int i = size - 1; i < n; i++)
	{
		for (int j = size - 1; j < m; j++)
		{
			if (get_hash(i, j, size) == get_hash_rev(i - size + 1, j - size + 1, size))
			{
			//	cerr << i << " " << j << " " << size << "\n";
				return true;
			}
		}
	}
	return false;
}

void solve()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			hash[i][j] = a[i][j] + 1;
			if (j) hash[i][j] += hash[i][j - 1] * Y;
			if (i) hash[i][j] += hash[i - 1][j] * X;
			if (i && j) hash[i][j] -= hash[i - 1][j - 1] * X * Y;
		}
	}
	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = m - 1; j >= 0; j--)
		{
			rhash[i][j] = a[i][j] + 1;
			if (j < m - 1) rhash[i][j] += rhash[i][j + 1] * Y;
			if (i < n - 1) rhash[i][j] += rhash[i + 1][j] * X;
			if (i < n - 1 && j < m - 1) rhash[i][j] -= rhash[i + 1][j + 1] * X * Y;
		}
	}
	pwx[0] = pwy[0] = 1;
	for (int i = 1; i <= max(n, m) + 1; i++)
	{
		pwx[i] = pwx[i - 1] * X;
		pwy[i] = pwy[i - 1] * Y;
	}
	int res = 1;

	vector <int> all;
	for (int i = 1; i <= min(n, m); i += 2)
		all.pb(i);
	int l = 0;
	int r = all.size() - 1;
	int mid;
	while (l <= r)
	{
		mid = (l + r) / 2;
		if (check(all[mid]))
		{
			res = max(res, all[mid]);
			l = mid + 1;
		}
		else r = mid - 1;
	}
	all.resize(0);
	for (int i = 2; i <= min(n, m); i += 2)
		all.pb(i);
	l = 0;
	r = all.size() - 1;
	while (l <= r)
	{
		mid = (l + r) / 2;
		if (check(all[mid]))
		{
			res = max(res, all[mid]);
			l = mid + 1;
		}
		else r = mid - 1;
	}
	
	cout << res;
}

#define file "matrix"
int main()
{
	freopen(file".in", "rt", stdin);
	freopen(file".out", "wt", stdout);
	load();
	solve();
	return 0;
}
